<template>
  <div class="app-container">

    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <!--      <el-tab-pane label="问卷调查" name="0"/>-->
      <!--      <el-tab-pane label="公投" name="1"/>-->

      <el-tab-pane v-for="type in surveyTempTypeOption"
                   :label="type.dictLabel"
                   :name="type.dictValue + ''">
      </el-tab-pane>
    </el-tabs>

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属物业" prop="propertyId">
        <el-select v-model="queryParams.propertyId" placeholder="请选择物业" size="mini">
          <el-option v-for="property in propertyListOptions"
                     :key="property.id"
                     :label="property.propertyName"
                     :value="property.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="模板标题" prop="questionTitle">
        <el-input v-model="queryParams.questionTitle" placeholder="请输入调查主题" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['module:survey:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['module:survey:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="surveyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="所在物业公司" align="center" prop="propertyName" />-->
      <el-table-column label="模板标题" align="center" prop="questionTitle" />
      <el-table-column label="二维码下载" align="center" width="150" v-if="activeName == '0'">
        <template slot-scope="scope">
          <el-popover placement="right" width="400" trigger="hover">
            <el-image style="width: 380px; height: 380px" :src="scope.row.qrCode"  fit="fill" />
            <i class="el-icon-picture" slot="reference"/>
          </el-popover>
          <el-button size="mini" type="text" icon="el-icon-download"
                     @click="downloadImg(scope.row)">{{scope.row.estateName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="问卷类型" align="center">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.surveyType === 0" style="cursor: pointer" :type="''">问卷调查</el-tag>
            <el-tag v-else style="cursor: pointer" :type=" '' ">模板</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="功能类型" align="center">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.functionType === 1" style="cursor: pointer" :type="''">巡检</el-tag>
            <el-tag v-if="scope.row.functionType === 2" style="cursor: pointer" :type="''">巡更</el-tag>
            <el-tag v-if="scope.row.functionType === 3" style="cursor: pointer" :type="''">保洁</el-tag>
            <el-tag v-if="scope.row.functionType === 4" style="cursor: pointer" :type="''">绿化</el-tag>
            <el-tag v-if="scope.row.functionType === 5" style="cursor: pointer" :type="''">消杀</el-tag>
            <el-tag v-if="scope.row.functionType === 6" style="cursor: pointer" :type="''">维保</el-tag>
            <el-tag v-if="scope.row.functionType === 7" style="cursor: pointer" :type="''">品质</el-tag>
            <el-tag v-if="scope.row.functionType === 8" style="cursor: pointer" :type="''">内部报事</el-tag>
            <el-tag v-if="scope.row.functionType === 9" style="cursor: pointer" :type="''">客户调查</el-tag>
          </div>
        </template>
      </el-table-column>

      <!--      <el-table-column label="来源类型" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          <div>-->
      <!--            <el-tag v-if="scope.row.fromType === 0" style="cursor: pointer" :type="''">平台发布</el-tag>-->
      <!--            <el-tag v-else style="cursor: pointer" :type=" '' ">个人发布</el-tag>-->
      <!--          </div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <!--      <el-table-column label="发布状态" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          <div>-->
      <!--            <el-tag v-if="scope.row.checkState === 0" style="cursor: pointer" :type="''">编辑中</el-tag>-->
      <!--            <el-tag v-else-if="scope.row.checkState === 1" style="cursor: pointer" :type="''">待审核</el-tag>-->
      <!--            <el-tag v-else-if="scope.row.checkState === 2" style="cursor: pointer" :type="'success'">审核通过</el-tag>-->
      <!--            <el-tag v-else style="cursor: pointer" :type=" 'info' ">审核驳回</el-tag>-->
      <!--          </div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type="''">正常</el-tag>
            <el-tag v-else style="cursor: pointer" :type=" 'info' ">停用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="400">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-s-tools">
            <router-link :to="'/property/survey/question?surveyId=' + scope.row.id" class="link-type">
              模板内容
            </router-link>
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-s-tools">
            <router-link :to="'/property/survey/record?surveyId=' + scope.row.id" class="link-type">
              模板记录
            </router-link>
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-picture" @click="resetQrCode(scope.row)"
                     v-hasPermi="['module:survey:edit']" v-if="activeName == '0'">生成二维码</el-button>
          <el-button size="mini"  type="text" v-if="scope.row.checkState === 1 && scope.row.surveyType === 1"
                     icon="el-icon-edit" @click="handleCheck(scope.row)" v-hasPermi="['module:survey:edit']">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:survey:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['module:survey:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改问卷调查对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="是否公开问卷" prop="isPublic">
          <el-radio-group v-model="form.isPublic" size="mini" @change="changePublic">
            <el-radio :value="0" :label="0">指定项目问卷</el-radio>
            <el-radio :value="1" :label="1">公开问卷</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属物业" prop="propertyId" v-if="form.isPublic === 0">
          <el-select v-model="form.propertyId" placeholder="请选择物业" size="mini">
            <el-option v-for="property in propertyListOptions"
                       :key="property.id"
                       :label="property.propertyName"
                       :value="property.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="所在小区" prop="estateId" v-if="form.isPublic === 0">
          <el-select v-model="form.estateId" placeholder="请选择小区" size="mini" filterable clearable>
            <el-option v-for="estate in estatesOptions"
                       :key="estate.id"
                       :label="estate.estateName"
                       :value="estate.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="功能类型" prop="functionType" v-if="form.isPublic === 0">
          <el-select v-model="form.functionType" placeholder="请选择功能类型" size="mini">
            <el-option v-for="dict in surveyFunctionTypeOption"
                       :key="parseInt(dict.dictValue)"
                       :label="dict.dictLabel"
                       :value="parseInt(dict.dictValue)"/>
          </el-select>
        </el-form-item>

        <el-form-item :label="labelTxt+'主题'" prop="questionTitle">
          <el-input v-model="form.questionTitle" :placeholder="'请输入'+labelTxt" size="mini"/>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state" size="mini">
            <el-radio :value="0" :label="0">停用</el-radio>
            <el-radio :value="1" :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="labelTxt +'内容'">
          <editor v-model="form.surveyInfo" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="titleCheck" :visible.sync="openCheck" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属物业" prop="propertyId">
          <el-select v-model="form.propertyId" placeholder="请选择物业" size="mini">
            <el-option v-for="property in propertyListOptions"
                       :key="property.id"
                       :label="property.propertyName"
                       :value="property.id"/>
          </el-select>
        </el-form-item>
        <el-form-item :label="labelTxt+'主题'" prop="questionTitle">
          <el-input v-model="form.questionTitle" :placeholder="'请输入'+labelTxt" size="mini"/>
        </el-form-item>
        <el-form-item label="发起人昵称" prop="nickName">
          {{form.nickName}}
        </el-form-item>
        <el-form-item label="审核状态" prop="checkState">
          <el-radio-group v-model="form.checkState" size="mini">
            <el-radio :value="0" :label="2">准许公布</el-radio>
            <el-radio :value="1" :label="3">禁用驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核结果" prop="checkResult">
          <el-input v-model="form.checkResult" type="textarea" rows="3" placeholder="请输入审核结果" size="mini"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="openCheck = false" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSurvey, getSurvey, delSurvey, addSurvey, updateSurvey,resetQrCode } from "@/api/module/survey/survey";
import { listPropertyNoPage } from "@/api/module/property/property";
import {listAllEstate} from "@/api/module/estate/estate";

export default {
  name: "Survey",
  data() {
    return {
      activeName:'0',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问卷调查表格数据
      surveyList: [],
      //物业公司列表
      propertyListOptions:[],
      surveyTempTypeOption:[],
      surveyFunctionTypeOption:[],
      estatesOptions:[],
      // 弹出层标题
      title: "",
      titleCheck:'',
      labelTxt:'问卷调查',
      // 是否显示弹出层
      open: false,
      openCheck:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        propertyId: undefined,
        surveyType:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.surveyType = 0;
    this.getList();
    this.getPropertyList();
    this.getEstateList();
    this.getDicts("survey_temp_type").then(response => {
      this.surveyTempTypeOption = response.data;
    });
    this.getDicts("survey_function_type").then(response => {
      this.surveyFunctionTypeOption = response.data;
    });
  },
  methods: {
    /** 查询问卷调查列表 */
    getList() {
      this.loading = true;
      listSurvey(this.queryParams).then(response => {
        this.surveyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getEstateList() {
      listAllEstate().then(response => {
        this.estatesOptions = response.data;
      });
    },
    changePublic(val){
      console.log('changePublic',val);
      if(val === 0){//指定项目

      }else{//公开问卷

      }
    },
    handleClick(val){
      console.log('handleClick',val.name);
      this.activeName = val.name;
      this.queryParams.surveyType = parseInt(val.name);
      this.getList();
    },
    getPropertyList() {
      listPropertyNoPage().then(response => {
        this.propertyListOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    downloadImg(row){
      window.location.href = process.env.VUE_APP_BASE_API + "/common/download/survey/" + row.id;
    },
    resetQrCode(row){
      var data = {
        id: row.id
      };
      resetQrCode(data).then(response => {
        if(response.code === 200){
          this.msgSuccess("生成成功");
          this.getList();
        }else{
          this.msgError("生成失败");
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        isPublic: 0,
        propertyId: undefined,
        surveyType:this.queryParams.surveyType,
        fromType:undefined,
        surveyInfo: undefined,
        functionType:undefined,
        questionTitle: undefined,
        question: undefined,
        answerSection: undefined,
        state: undefined,
        createTime: undefined,
        checkState:undefined,
        checkSysUserId:undefined,
        checkSysUserName:undefined,
        checkResult:undefined,
        checkTime:undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      if(this.queryParams.surveyType == 0){
        this.title = "添加问卷调查";
        this.labelTxt = '问卷调查';
      }else{
        this.title = "添加模板";
        this.labelTxt = '模板';
      }
    },
    /** 修改按钮操作 */
    handleCheck(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getSurvey(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.openCheck = true;
        if(this.queryParams.surveyType == 0){
          this.titleCheck = "修改问卷调查";
          this.labelTxt = '问卷调查';
        }else{
          this.titleCheck = "修改模板";
          this.labelTxt = '模板';
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getSurvey(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        if(this.queryParams.surveyType == 0){
          this.title = "修改问卷调查";
          this.labelTxt = '问卷调查';
        }else{
          this.title = "修改模板";
          this.labelTxt = '模板';
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.isPublic === 1){//指定项目
            this.form.propertyId = 0;
            this.form.estateName = 0;
          }

          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSurvey(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.openCheck = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSurvey(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.openCheck = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除问卷调查编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delSurvey(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
